/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backingbeans;

import entities.UUser;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import sessions.UserManager;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Named(value = "userSession")
@SessionScoped
public class UserSession implements Serializable {
    @EJB
    private UserManager userManager;
    private boolean connected;
    private UUser user;

    public boolean isConnected() {
        return connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }

    public UUser getUser() {
        // Be sure to get an user object up to date
        user = userManager.getUser(user.getId());
        return user;
    }

    public void setUser(UUser user) {
        this.user = user;
    }

    public void login(UUser user) {
        this.user = user;
        connected = true;
    }

    public void logout() {
        user = null;
        connected = false;
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    }

}
